package com.dailyblue.java.spring.boot.redis.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/list")
@Slf4j
public class ListController {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/a")
    public String a() {
        ListOperations<String, String> list = stringRedisTemplate.opsForList();
        list.leftPush("A", "aa");
        list.leftPushAll("A", "bb", "cc", "dd", "ee", "ff", "gg");
        list.rightPush("A", "11");
        list.rightPushAll("A", "22", "33", "44", "55", "66", "77", "88");
        return "";
    }

    @GetMapping("/b")
    public String b() {
        ListOperations<String, String> list = stringRedisTemplate.opsForList();
        List<String> allList1 = list.range("A", 0, -1);
        String l1 = list.leftPop("A");
        String l2 = list.rightPop("A");
        List<String> lefts = list.leftPop("A", 3);
        List<String> rights = list.rightPop("A", 2);
        List<String> allList2 = list.range("A", 0, -1);
        String v1 = list.index("A", 3);
        log.info("获取所有数据1:{}", allList1);
        log.info("l1:{}", l1);
        log.info("l2:{}", l2);
        log.info("lefts:{}", lefts);
        log.info("rights:{}", rights);
        log.info("获取所有数据2:{}", allList2);
        log.info("v:{}", v1);
        return "";
    }

    @GetMapping("/c")
    public String c() {
        ListOperations<String, String> list = stringRedisTemplate.opsForList();
        list.remove("A", 2, "dd");
        list.set("A", 3, "MMMMM");
        long size = list.size("A");
        System.out.println(list+".....");
        Long index1 = list.indexOf("A", "a");
        Object index2 = list.indexOf("A", "aa");
        log.info("size:{},index1:{},index:{}", size, index1, index2);
        return "";
    }
}
